package me.twoday.library.mine.common.parser;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import me.twoday.library.mine.handler.SaxDataHandler;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.util.Log;

/**
 * SAX파서를 손쉽게 사용하기위해 제공되는 클래스 
 *
 * @author 김학성
 * @since 2012. 5. 18.
 */
@SuppressWarnings("rawtypes")
public class SaxHandler 
{
	public static final String TAG = "SaxHandler";
	public static ArrayList<?> getSaxListData(String _url, SaxDataHandler _saxDataHandler)
	{
		try
		{
			SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
			SAXParser saxP = saxPF.newSAXParser();
			XMLReader xmlR = saxP.getXMLReader();
			URL url = new URL(_url); // URL of the XML
			SaxDataHandler myDataHandler = _saxDataHandler;
			xmlR.setContentHandler(myDataHandler);
			xmlR.parse(new InputSource(url.openStream()));
			return myDataHandler.getDataList();
		}catch (Exception e) {
			Log.e(TAG, e.toString());
		}
		return null;
	}
}
